package com.rc.evcharger.redis;

import com.rc.evcharger.common.redis.RedisDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RedOrderServiceImpl implements RedOrderService {

    @Autowired
    private RedisDao redisDao;

    @Override
    public void addOrder(String rtua, String gunNum, String mobile, String orderNum) {
        RedOrder order = new RedOrder(rtua, gunNum, orderNum, orderNum);
        //redisDao.putSession(order.getKey(), order.getValue());
        redisDao.putRedis(order.getKey(), String.valueOf(order.getValue()));
    }

    @Override
    public Long getOrderNum(String rtua, String gunNum, String orderNum) {
        RedOrder order = new RedOrder(rtua, gunNum, orderNum);

        String key = order.getKey();
        String str = redisDao.getRedis(order.getKey());
        if (str == null) {
            return null;
        }
        return Long.valueOf(str);
    }

    @Override
    public void removeOrder(String rtua, String gunNum, String orderNum) {
        RedOrder order = new RedOrder(rtua, gunNum, orderNum);
        redisDao.deleteSession(order.getKey());
    }

    @Override
    public boolean hasOrder(String rtua, String gunNum, String orderNum) {
        RedOrder order = new RedOrder(rtua, gunNum, orderNum);
        List list = (List) redisDao.keys(order.getKey() + "*");
        if (list.size() > 0) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        RedOrder order = new RedOrder("19 00 11 00", "1", "15200994525", "18529447738asdas");
        //redisDao.putSession(order.getKey(), order.getValue());
        System.out.println(order.getKey());
    }
}
